#include "sw_i2c.h"
#include "adc_mcp3421.h"
#include "bsp_uart.h"

#define MCP_Add_Write 0xD0
#define MCP_Add_Read 0xD1

MCP3421 mcp3421 = {0};
// MCPData: 0 - 131071, 0-2048mv

/********************************
 * Bit 7: 
 * 		Read:	1 = Output register has not been updated. 
 * 				0 = Output register has been updated with the latest conversion result.
 * 		Wirte:
 * 				Continuous Conversion mode: No effect One-Shot
 * 				Conversion mode:
 * 				1 = Initiate a new conversion.
 * 				0 = No effect.
 * Bit 6 - 5:
 * 		These bits are not effected for the MCP3421.
 * Bit 4: 
 * 		Conversion Mode Bit
 * 		1 = Continuous Conversion Mode (Default).
 * 			The device performs data conversions continuously.
 * 		0 = One-Shot Conversion Mode.
 * 			The device performs a single conversion and enters a low power
 * 			standby mode until it receives another write or read command.
 * Bit 3-2:
 * 			Sample Rate Selection Bit 
 * 			00 = 240 SPS (12 bits) (Default) 
 * 			01 = 60 SPS (14 bits) 
 * 			10 = 15 SPS (16 bits) 
 * 			11 = 3.75 SPS (18 bits)
 * 
 * Bit 1-0:
 * 			G1-G0: PGA Gain Selection Bits
 * 			00 = x1 (Default) 
 * 			01 = x2
 * 			10 = x4
 * 			11 = x8
 * 
 * 
 * ******************************
*/
void MCP3421_Configure(uint8_t autoConversion, uint8_t sampleRate, uint8_t gain)
{
	uint8_t mode = 0;
	mcp3421.continuous = autoConversion;
	mcp3421.sampleRate = sampleRate;
	mcp3421.pgaGain = gain;
	mode = autoConversion | sampleRate | gain;
	SW_I2C_start();
	SW_I2C_write(MCP_Add_Write);
	if(SW_I2C_wait_ACK()==0)
	{
		Queue_Print(&htx1, "Set Mode is faliure,MCP3421 no ACK\n\r");
		return;
	}
	SW_I2C_write(mode);
	SW_I2C_wait_ACK();
	SW_I2C_stop();
	
}

/**************************
    18-bits: MMMMMMD17D16 (1st data byte) - D15 ~ D8 (2nd data byte) - D7 ~ D0 (3rd data byte) - Configuration byte. (Note 1)
	16-bits D15 ~ D8 (1st data byte) - D7 ~ D0 (2nd data byte) - Configuration byte. (Note 2)
	14-bits MMD13D ~ D8 (1st data byte) - D7 ~ D0 (2nd data byte) - Configuration byte. (Note 3)
	12-bits MMMMD11 ~ D8 (1st data byte) - D7 ~ D0 (2nd data byte) - Configuration byte. (Note 4)

	Note 1: D17 is MSB (= sign bit), M is repeated MSB of the data byte.
	2: D15 is MSB (= sign bit).
	3: D13 is MSB (= sign bit), M is repeated MSB of the data byte.
	4: D11 is MSB (= sign bit), M is repeated MSB of the data byte
**************************/
uint32_t MCP3421_RefreshData(void)
{
	static uint8_t adc_data[4];
	double ADCValue = 0;
	SW_I2C_start();
	SW_I2C_write(MCP_Add_Read);
	if(SW_I2C_wait_ACK()==0)
	{
		Queue_Print(&htx1, "Reflash Data is faliure,MCP3421 no ACK\n\r");
		return 0;
	}
	if (mcp3421.sampleRate == MCP_RATE_3_75SPS_18BIT)
	{
		adc_data[0]=SW_I2C_read(1);
		adc_data[1]=SW_I2C_read(1);
		adc_data[2]=SW_I2C_read(1);
		adc_data[3]=SW_I2C_read(0);	// config bit check
		SW_I2C_stop();
		ADCValue = (uint32_t) adc_data[0] << 16 | (uint32_t) adc_data[1] << 8 | (uint32_t) adc_data[2];
		if (ADCValue > 0x3ffff) {
			ADCValue = 0;
		}
	}
	else
	{
		adc_data[0]=SW_I2C_read(1);
		adc_data[1]=SW_I2C_read(1);
		adc_data[3]=SW_I2C_read(0);	// config bit check
		SW_I2C_stop();
		if (mcp3421.sampleRate == MCP_RATE_15SPS_16BIT)
		{
			ADCValue = (uint32_t) adc_data[0] << 8 | (uint32_t) adc_data[1];
		}
		if (mcp3421.sampleRate == MCP_RATE_60SPS_14BIT)
		{
			ADCValue = (uint32_t) adc_data[0] << 8 | (uint32_t) adc_data[1];
			if (ADCValue > 0x3fff) {
				ADCValue = 0;
			}
		}
		if (mcp3421.sampleRate == MCP_RATE_240SPS_12BIT)
		{
			ADCValue = (uint32_t) adc_data[0] << 8 | (uint32_t) adc_data[1];
			if (ADCValue > 0x0fff) {
				ADCValue = 0;
			}
		}
	}
	return ADCValue;

}

double MCP3421_GetVoltage(void)
{
	double voltage = MCP3421_RefreshData() * 2.048;
	switch (mcp3421.sampleRate)
	{
	case MCP_RATE_3_75SPS_18BIT:
		voltage /= 1<<17;
		break;
	case MCP_RATE_15SPS_16BIT:
		voltage /= 1<<15;
		break;
	case MCP_RATE_60SPS_14BIT:
		voltage /= 1<<13;
		break;
	case MCP_RATE_240SPS_12BIT:
		voltage /= 1<<11;
		break;
	default:
		break;
	}
	return mcp3421.voltage = voltage;
}

